package com.glsc.ngateway.common.base.repo.mysql.opmanage.report.fileupload;

import com.glsc.ngateway.common.base.domain.mysql.opmanage.report.fileupload.FileUploadRelateImport;
import com.glsc.ngateway.common.base.dto.opmanage.report.ImportFileDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface FileUploadRelateImportRepo extends JpaRepository<FileUploadRelateImport, String>, JpaSpecificationExecutor<FileUploadRelateImport> {

    @Transactional
    @Modifying
    @Query(value = "update FileUploadRelateImport f set f.isDel = 1 where f.id = :id")
    int deleteById(@Param("id") long id);

    @Transactional
    @Modifying
    @Query(value = "update FileUploadRelateImport f set f.isDel = 1 where f.id in (:idList)")
    int deleteByIdList(@Param("idList") List<Long> idList);

    @Query(value = "select max(t.id) from FileUploadRelateImport t")
    Long queryMaxId();

    @Transactional
    @Modifying
    @Query(value = "select f from FileUploadRelateImport f where f.fileId = :fileId")
    FileUploadRelateImport findByFileId(@Param("fileId") String fileId);

    @Query(value = "select new com.glsc.ngateway.common.base.dto.opmanage.report.ImportFileDto(t1.id,t1.fileId,t2.fileName,t2.userName,t2.uploadTime) " +
            "from FileUploadRelateImport t1, \n" +
            "       FileUploadLog t2\n" +
            " where t1.catId  = :catId\n" +
            "   and t1.bizdate = :bizdate\n" +
            "   and t1.isDel = 0\n" +
            "   and t2.id = t1.fileId\n" +
            "   and (t1.dimension1 is null or t1.dimension1 = :dimension1)")
    List<ImportFileDto> queryImportFile(@Param("catId") String catId,
                                        @Param("bizdate") String bizdate,
                                        @Param("dimension1") String dimension1);

    @Query(value = "select distinct t.bizdate from FileUploadRelateImport t \n" +
            " where t.catId  = :catId\n" +
            "   and t.isDel = 0 order by t.bizdate asc")
    List<String> queryBizdateByCatId(@Param("catId") String catId);
}
